package jsyf.oa.web.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.system.WechatShareDetailFacade;
import jsyf.oa.facade.system.WechatShareFacade;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by WHH-PC on 2018/1/12.
 */
@Api(value = "微信分享", description = "微信分享")
@Controller
@RequestMapping("/wechatShare")
public class WechatShareController {
    @Resource
    private WechatShareFacade wechatShareFacade;
    @Resource
    private WechatShareDetailFacade wechatShareDetailFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "分享链接信息录入",
            notes = "分享链接信息录入",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "", dataType = "String", paramType = "body")
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String id = wechatShareFacade.save(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("id", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

    @RequestMapping(value = "/saveShareDetail.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "分享后信息录入",
            notes = "分享后信息录入",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{wechatShareId:\"\",userId:\"201712182045404A99D3141F0994413E\"}", dataType = "String", paramType = "body")
    public void saveShareDetail(HttpServletResponse response, @RequestBody String jsonData) {
        String id = wechatShareDetailFacade.save(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("id", id);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

    @RequestMapping(value = "/getWechatShare.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取需要分享的链接",
            notes = "获取需要分享的链接",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{shareDate:\"2018-01-12\",userId:\"201712182045404A99D3141F0994413E\"}", dataType = "String", paramType = "body")
    public void getWechatShare(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String shareDate = obj.getString("shareDate");
        String userId = obj.getString("userId");
        String companyId = obj.getString("companyId");
        List<JSONObject> records = this.wechatShareFacade.getWechatShare(shareDate, userId, companyId);
        if (records != null) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
        }
    }

    @RequestMapping(value = "/saveTodayWechatShare.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "生成某天的微信分享链接",
            notes = "生成某天的微信分享链接",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    public void saveTodayWechatShare(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String companyId = obj.getString("companyId");
        String userId = obj.getString("userId");
        String shareDate = obj.getString("shareDate");
        String result = this.wechatShareFacade.saveTodayWechatShare(companyId, userId, shareDate);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "删除微信分享链接",
            notes = "删除微信分享链接",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{id:\"201712182045404A99D3141F0994413E\"}", dataType = "String", paramType = "body")
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject obj = JSON.parseObject(jsonData);
        String id = obj.getString("id");
        String result = this.wechatShareFacade.delById(id);
        ResponseUtils.putJsonResponse(response, result);
    }

//    @RequestMapping(value = "/getWechatShareByUserId.json", method = RequestMethod.POST)
//    @ApiOperation(
//            value = "微信转发报表-区域经理",
//            notes = "微信转发报表-区域经理",
//            httpMethod = "POST",
//            produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiImplicitParam(name = "jsonData", value = "{companyId:\"2017121617464D12B1025EC2259267D2\",shareDate:\"2018-01-27\",userId:\"20171216194740DA8016D90111D63789\"}\n按日期查询shareDate格式：2018-01-12，按月份查询shareDate格式：2018-01", dataType = "String", paramType = "body")
//    public void getWechatShareByUserId(HttpServletResponse response, @RequestBody String jsonData) {
//        JSONObject obj = JSON.parseObject(jsonData);
//        String companyId = obj.getString("companyId");
//        String shareDate = obj.getString("shareDate");
//        String userId = obj.getString("userId");
//        JSONObject result = this.wechatShareFacade.getWechatShareByUserId(companyId, shareDate, userId);
//        if (result != null) {
//            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
//        } else {
//            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
//        }
//    }
//
//    @RequestMapping(value = "/getWechatShareBigRegion.json", method = RequestMethod.POST)
//    @ApiOperation(
//            value = "微信转发报表-大区经理",
//            notes = "微信转发报表-大区经理",
//            httpMethod = "POST",
//            produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiImplicitParam(name = "jsonData", value = "{companyId:\"2017121617464D12B1025EC2259267D2\",shareDate:\"2018-01-27\",userId:\"20171216194740DA8016D90111D63789\"}\n按日期查询shareDate格式：2018-01-12，按月份查询shareDate格式：2018-01", dataType = "String", paramType = "body")
//    public void getWechatShareBigRegion(HttpServletResponse response, @RequestBody String jsonData) {
//        JSONObject obj = JSON.parseObject(jsonData);
//        String companyId = obj.getString("companyId");
//        String shareDate = obj.getString("shareDate");
//        String userId = obj.getString("userId");
//        JSONObject result = this.wechatShareFacade.getWechatShareByBigRegion(companyId, shareDate, userId);
//        if (result != null) {
//            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
//        } else {
//            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
//        }
//    }
//
//    @RequestMapping(value = "/getWechatShareSupport.json", method = RequestMethod.POST)
//    @ApiOperation(
//            value = "微信转发报表-区域支持",
//            notes = "微信转发报表-区域支持",
//            httpMethod = "POST",
//            produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiImplicitParam(name = "jsonData", value = "{companyId:\"2017121617464D12B1025EC2259267D2\",shareDate:\"2018-01-27\",userId:\"201712161955459C99191A616650E90F\"}\n按日期查询shareDate格式：2018-01-12，按月份查询shareDate格式：2018-01", dataType = "String", paramType = "body")
//    public void getWechatShareSupport(HttpServletResponse response, @RequestBody String jsonData) {
//        JSONObject obj = JSON.parseObject(jsonData);
//        String companyId = obj.getString("companyId");
//        String shareDate = obj.getString("shareDate");
//        String userId = obj.getString("userId");
//        JSONObject result = this.wechatShareFacade.getWechatShareBySupport(companyId, shareDate, userId);
//        if (result != null) {
//            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
//        } else {
//            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
//        }
//    }
//
//    @RequestMapping(value = "/getWechatShareCompany.json", method = RequestMethod.POST)
//    @ApiOperation(
//            value = "微信转发报表-公司管理层",
//            notes = "微信转发报表-公司管理层",
//            httpMethod = "POST",
//            produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiImplicitParam(name = "jsonData", value = "{companyId:\"2017121617464D12B1025EC2259267D2\",shareDate:\"2018-01-27\"}\n按日期查询shareDate格式：2018-01-12，按月份查询shareDate格式：2018-01", dataType = "String", paramType = "body")
//    public void getWechatShareCompany(HttpServletResponse response, @RequestBody String jsonData) {
//        JSONObject obj = JSON.parseObject(jsonData);
//        String companyId = obj.getString("companyId");
//        String shareDate = obj.getString("shareDate");
//        JSONObject result = this.wechatShareFacade.getWechatShareByCompanyId(companyId, shareDate);
//        if (result != null) {
//            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
//        } else {
//            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
//        }
//    }
//
//    @RequestMapping(value = "/getWechatShareGroup.json", method = RequestMethod.POST)
//    @ApiOperation(
//            value = "微信转发报表-集团管理层",
//            notes = "微信转发报表-集团管理层",
//            httpMethod = "POST",
//            produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiImplicitParam(name = "jsonData", value = "{shareDate:\"2018-01-27\"}\n按日期查询shareDate格式：2018-01-12，按月份查询shareDate格式：2018-01", dataType = "String", paramType = "body")
//    public void getWechatShareGroup(HttpServletResponse response, @RequestBody String jsonData) {
//        JSONObject obj = JSON.parseObject(jsonData);
//        String shareDate = obj.getString("shareDate");
//        JSONObject result = this.wechatShareFacade.getWechatShareByGroup(shareDate);
//        if (result != null) {
//            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(result));
//        } else {
//            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("获取数据错误"));
//        }
//    }


}
